package com.workcell.workdata.xthings.utils;

import lombok.extern.slf4j.Slf4j;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @Author 廖博文
 * @Date 2022/12/6 14:55
 * @Description
 */
@Slf4j
public class DateTimeUtil {

	/**
	 * 增加小时并且格式分钟和秒数
	 * @param hours
	 * @return
	 */
	public static Date addHourAndFormatSM(Integer hours){
		Calendar calendar = Calendar.getInstance();
		calendar.set(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)
				,calendar.get(Calendar.HOUR_OF_DAY)+hours,0,0);
		return calendar.getTime();
	}

	/**
	 * 增加小时并且格式小时、分钟和秒数
	 * @param day
	 * @return
	 */
	public static Date addDayAndFormatSM(Integer day){
		Calendar calendar = Calendar.getInstance();
		calendar.set(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH) + day
				,0,0,0);
		return calendar.getTime();
	}

	/**
	 * 根据时间添加小时
	 * @param dateStr
	 * @param hour
	 * @return
	 */
	public static Date dateAddHour(String dateStr, Integer hour){
		Date date = null;
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			date = format.parse(dateStr);
		} catch (Exception ex) {
			log.info("dateAddHour 转换异常。");
			ex.printStackTrace();
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.HOUR, hour);
		return cal.getTime();
	}
}
